# Copyright 2021 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================

load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test", "pkg_npm")
load("//tools:copy_to_dist.bzl", "copy_to_dist", "copy_ts_library_to_dist")
load("//tools:tfjs_bundle.bzl", "tfjs_bundle")
load("//tools:tfjs_web_test.bzl", "tfjs_web_test")

package(default_visibility = ["//visibility:public"])

# Allow typescript rules in any package to reference this file
exports_files([
    "package.json",
    "README.md",
])

nodejs_test(
    name = "tfjs-data_test",
    data = [
        "//tfjs-backend-cpu/src:tfjs-backend-cpu_lib",
        "//tfjs-data/src:tfjs-data_test_lib",
    ],
    entry_point = "//tfjs-data/src:test_node.ts",
    tags = ["ci"],
)

# Defined here because chdir must be a subdirectory of the directory the rule is
# defined in.
nodejs_test(
    name = "test_snippets_test",
    chdir = "tfjs-data",
    data = [
        ":tsconfig.json",
        "//:tsconfig.json",
        "//tfjs-backend-cpu/src:tfjs-backend-cpu_lib",
        "//tfjs-data/scripts:test_snippets_lib",
        "//tfjs-data/src:all_srcs",
    ],
    entry_point = "//tfjs-data/scripts:test_snippets.ts",
    tags = ["ci"],
)

tfjs_web_test(
    name = "tfjs-data_browser_test",
    srcs = [
        "//tfjs-data/src:tfjs-data_test_bundle",
    ],
    browsers = [
        "bs_chrome_mac",
        "win_10_chrome",
    ],
    local_browser = "chrome_autoplay",
    static_files = [
        # Listed here so sourcemaps are served
        "//tfjs-data/src:tfjs-data_test_bundle",
    ],
)

tfjs_bundle(
    name = "tf-data",
    entry_point = "//tfjs-data/src:index.ts",
    external = [
        "@tensorflow/tfjs-core",
    ],
    globals = {
        "@tensorflow/tfjs-core": "tf",
    },
    umd_name = "tf",
    deps = [
        "//tfjs-data/src:tfjs-data_lib",
        "//tfjs-data/src:tfjs-data_src_lib",
    ],
)

copy_ts_library_to_dist(
    name = "copy_src_to_dist",
    srcs = [
        "//tfjs-data/src:tfjs-data_lib",
        "//tfjs-data/src:tfjs-data_src_lib",
    ],
    root = "src",
)

copy_to_dist(
    name = "copy_bundles",
    srcs = [
        ":tf-data",
        ":tf-data.es2017",
        ":tf-data.es2017.min",
        ":tf-data.fesm",
        ":tf-data.fesm.min",
        ":tf-data.min",
        ":tf-data.node",
    ],
)

copy_file(
    name = "copy_miniprogram",
    src = ":tf-data.min.js",
    out = "dist/miniprogram/index.js",
)

copy_file(
    name = "copy_miniprogram_map",
    src = ":tf-data.min.js.map",
    out = "dist/miniprogram/index.js.map",
)

pkg_npm(
    name = "tfjs-data_pkg",
    package_name = "@tensorflow/tfjs-data",
    srcs = [
        "README.md",
        "package.json",
    ],
    tags = ["ci"],
    deps = [
        ":copy_bundles",
        ":copy_miniprogram",
        ":copy_miniprogram_map",
        ":copy_src_to_dist",
    ],
)

test_suite(
    name = "tests",
    tests = [
        ":tfjs-data_browser_test",
        ":tfjs-data_test",
    ],
)
